package com.bl.student.api.controller;

import com.bl.student.common.ApiResponseBody;
import com.bl.student.dao.model.CityEntity;
import com.bl.student.service.biz.main.MainService;
import com.bl.student.service.biz.message.MessageService;
import com.bl.student.service.vo.ListVo;
import com.bl.student.service.vo.main.*;
import io.swagger.annotations.*;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import static org.springframework.web.bind.annotation.RequestMethod.GET;

/**
 * Created by yujingyi on 2018/1/17.
 */
@RestController
@Log4j2
@RequestMapping(value = {"/student/apis"})
@Api(value = "注册登陆相关api", description = "注册登陆相关api")
public class MainController extends BaseController {

    static final private String OPEN_ID_COOKIE_NAME = "OPEN_ID";
    static final private String MOBILE_COOKIE_NAME = "MOBILE";
    static final private String UNION_ID_COOKIE_NAME = "UNION_ID";
    static final private String USER_ID_COOKIE_NAME = "USER_ID";

    @Autowired
    MainService mainService;

    @Autowired
    MessageService messageService;

    @ApiOperation(value = "获取短信验证码", notes = "获取短信验证码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "mobile", value = "手机号", required = true, paramType = "query", dataType = "String")})
    @GetMapping(value = {"/get_captcha"})
    public ApiResponseBody getCaptcha(
            @RequestParam(value = "mobile", required = true) String mobile,
            HttpServletRequest request) {

        messageService.sendCaptchaMessage(mobile);

        return ApiResponseBody.createSuccessBody(null);
    }

    @ApiOperation(value = "注册接口", notes = "")
    @PostMapping(value = {"/register"})
    @ResponseBody
    public ApiResponseBody register(
            @Valid @RequestBody RegisterVo body, BindingResult result,
            HttpServletRequest request, HttpServletResponse response) {

        ApiResponseBody validResult = validParams(result);
        if (validResult != null) {
            return validResult;
        }

        LoginResponseVo loginResponseVo = mainService.register(body, getOpenId(request));

        // set cookie
        Cookie tokenCookie = new Cookie(MOBILE_COOKIE_NAME, loginResponseVo.getMobile());
        tokenCookie.setPath("/");
        Cookie accountCookie = new Cookie(UNION_ID_COOKIE_NAME, loginResponseVo.getUnionId());
        accountCookie.setPath("/");
        Cookie openIdCookie = new Cookie(OPEN_ID_COOKIE_NAME, loginResponseVo.getOpenId());
        openIdCookie.setPath("/");
        Cookie userIdCookie = new Cookie(USER_ID_COOKIE_NAME, loginResponseVo.getId());
        userIdCookie.setPath("/");

        response.addCookie(tokenCookie);
        response.addCookie(accountCookie);
        response.addCookie(userIdCookie);
        response.addCookie(openIdCookie);

        return ApiResponseBody.createSuccessBody(loginResponseVo);
    }

    @ApiOperation(value = "注册环形接口", notes = "")
    @PostMapping(value = {"/register_hx"})
    @ResponseBody
    public ApiResponseBody registerHX(
            @Valid @RequestBody RegisterVo body, BindingResult result,
            HttpServletRequest request, HttpServletResponse response) {

        ApiResponseBody validResult = validParams(result);
        if (validResult != null) {
            return validResult;
        }

        mainService.register(body);


        return ApiResponseBody.createSuccessBody(null);
    }

    @ApiOperation(value = "登陆", notes = "sid不同时会重新登陆")
    @PostMapping(value = {"/login"})
    public ApiResponseBody<LoginResponseVo> login(
            @Valid @RequestBody LoginVo body, BindingResult result,
            HttpServletRequest request , HttpServletResponse response) {

        ApiResponseBody validResult = validParams(result);
        if (validResult != null) {
            return validResult;
        }

        LoginResponseVo loginResponseVo = mainService.appLogin(body, getUnionId(request));

        // set cookie
        Cookie tokenCookie = new Cookie(MOBILE_COOKIE_NAME, loginResponseVo.getMobile());
        tokenCookie.setPath("/");
        tokenCookie.setMaxAge(3600 * 24 * 30);
        Cookie accountCookie = new Cookie(UNION_ID_COOKIE_NAME, loginResponseVo.getUnionId());
        accountCookie.setPath("/");
        Cookie openIdCookie = new Cookie(OPEN_ID_COOKIE_NAME, loginResponseVo.getOpenId());
        openIdCookie.setPath("/");
        Cookie userIdCookie = new Cookie(USER_ID_COOKIE_NAME, loginResponseVo.getId());
        userIdCookie.setPath("/");

        response.addCookie(tokenCookie);
        response.addCookie(accountCookie);
        response.addCookie(userIdCookie);
        response.addCookie(openIdCookie);

        return ApiResponseBody.createSuccessBody(loginResponseVo);
    }

    @ApiOperation(value = "小程序登陆接口", notes = "用于公众平台中点击用户登陆后调用")
    @RequestMapping(value = {"/mini_wechat_login"}, method = {GET})
//    @ResponseStatus(HttpStatus.OK)
    public ApiResponseBody<LoginResponseVo> miniWechatLogin(
            @ApiParam(required = true, value = "换取access_token的code")
            @RequestParam(value = "code", required = true) String code,
            @ApiParam(required = true, value = "第三方程序发送时用来标识其请求的唯一性的标志，由第三方程序调用sendReq时传入，由微信终端回传，state字符串长度不能超过1K")
            @RequestParam(value = "encryptedData", required = true) String encryptedData,
            @ApiParam(required = true, value = "跳转页面的url")
            @RequestParam(value = "iv", required = true) String iv,
            HttpServletRequest request, HttpServletResponse response) {

        LoginResponseVo loginResponseVo = mainService.miniWechatLogin(code, encryptedData, iv);

        // set cookie
        Cookie tokenCookie = new Cookie(MOBILE_COOKIE_NAME, loginResponseVo.getMobile());
        tokenCookie.setPath("/");
        tokenCookie.setMaxAge(3600 * 24 * 30);
        Cookie accountCookie = new Cookie(UNION_ID_COOKIE_NAME, loginResponseVo.getUnionId());
        accountCookie.setPath("/");
        Cookie openIdCookie = new Cookie(OPEN_ID_COOKIE_NAME, loginResponseVo.getOpenId());
        openIdCookie.setPath("/");
        Cookie userIdCookie = new Cookie(USER_ID_COOKIE_NAME, loginResponseVo.getId());
        userIdCookie.setPath("/");

        response.addCookie(openIdCookie);

        return ApiResponseBody.createSuccessBody(loginResponseVo);
    }

    @ApiOperation(value = "修改密码", notes = "")
    @PostMapping(value = {"/reset"})
    public ApiResponseBody reset(
            @Valid @RequestBody ResetVo body, BindingResult result,
            HttpServletRequest request, HttpServletResponse response) {
        ApiResponseBody validResult = validParams(result);
        if (validResult != null) {
            return validResult;
        }
        mainService.reset(body);
        return ApiResponseBody.createSuccessBody(null);
    }

    @ApiOperation(value = "登出", notes = "")
    @ApiImplicitParams({})
    @GetMapping(value = {"/logout"})
    public ApiResponseBody logout(
            HttpServletRequest request, HttpServletResponse response) {
        mainService.logout(getUserInfo(request));
        return ApiResponseBody.createSuccessBody(null);
    }

}
